package org.openit.portail.service.impl;

import java.util.Date;
import java.util.List;

import org.openit.portail.dao.ClientDao;
import org.openit.portail.dao.UserSystemDao;
import org.openit.portail.model.Client;
import org.openit.portail.service.ClientService;
import org.openit.portail.service.UtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class ClientServiceImpl implements ClientService {
	
	@Autowired(required=true)
	private ClientDao clientDao;
	@Autowired
	private UserSystemDao systemDao;
	@Autowired UtilService utilService;
	
	public void saveOrUpdateClient(org.openit.portail.model.Client client) throws Exception {
		if(systemDao.getByEmail(client.getEmail())!=null){
			throw new Exception();
			
		}
		if(client.getId()==0){
			client.setDtCreation(new Date());
			client.setStatus(0);
			utilService.sendMail(client);
		}
			client.setDtModification(new Date());
			clientDao.save(client);
		
	}

	
	public List<Client> getClients() {
		// TODO Auto-generated method stub
		return clientDao.findAll();
	}

	
	public Client getClient(int id) {
		// TODO Auto-generated method stub
		return clientDao.get((long)id);
	}

	
	public void deleteClient(Client client) {
		clientDao.delete(client);
	}

	
	public void activateClient(Client client) throws Exception {
		client.setStatus(1);
		saveOrUpdateClient(client);
	}
	
}
